AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')
local Ground = 1 + 0 + 2 + 8 + 32
local rel = "vehicles/tank_readyfire1.wav"


function ENT:Initialize()
	self.orginalposition = self.Entity:GetPos()
	self.Entity:SetModel(  "models/props_lab/tpplugholder_single.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType(  MOVETYPE_VPHYSICS   )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	local pos = self.Entity:GetPos()
	self.fire = false
	self.use = false
end

function ENT:SpawnFunction( ply, tr )
	if ( !tr.Hit ) then return end
	local SpawnPos = tr.HitPos + tr.HitNormal * 36
	local ent = ents.Create( "autoturret1" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end

function ENT:GetEnemyandFire()
	self.enemy = nil
	local proppos = self.core:GetPos()
	if (CheckPoint(proppos.x,proppos.y,proppos.z,"Station") or CheckPoint(proppos.x,proppos.y,proppos.z,"StationBig")) then 
		return 
	end
	if not self.plyuni then
		self.plyuni = servertable["entitys"][self.Entity:EntIndex()]["owner"]
	end	
	for index,ply in pairs(servertable["ply"][self.plyuni]["friendenemy"]["enemys"]) do	
		if player.GetByUniqueID(ply) then
			if player.GetByUniqueID(ply):IsValid() then
				self.enemy = ply
			end
		end
		if self.enemy && self.plyuni then
			if (servertable["ply"][self.enemy]["currentsector"] and servertable["ply"][self.plyuni]["currentsector"]) then
				if (servertable["ply"][self.enemy]["currentsector"] == servertable["ply"][self.plyuni]["currentsector"]) then
					for k,core in pairs(servertable["ship"]["shipcores"]) do  
						if (GetOwnStatus(plyuni,core) == 1) then
							local proppos = core:GetPos()
							if not (CheckPoint(proppos.x,proppos.y,proppos.z,"Station") or CheckPoint(proppos.x,proppos.y,proppos.z,"StationBig")) then
								Msg("Found enemy")
								local pos = self.Entity:GetPos()
								local corepos = core:GetPos()
								local move = Vector(0,0,0)
								local aimangle = (corepos - pos):Normalize()
								self.Angles = aimangle:Angle( )
								self.pos = pos + move
								local Startpos =  self.Entity:GetPos() + (aimangle * 50) 
								if (self.core.capaciator > 200) then
									if (self.core.bullets > 1) then
										if Fieldoffire(self.Entity,100) then
											self.core.bullets = self.core.bullets -1
											self.core.capaciator = self.core.capaciator -200
											bullet = {} 
											bullet.Src			= Startpos
											bullet.Dir			= aimangle
											bullet.Spread		= Vector(0.05,0.05,0.05) 
											bullet.Num			= 1 
											bullet.Damage		= 200 
											bullet.Force		= 1 
											bullet.Tracer		= 1
											bullet.TracerName	= "Tracer"
											self.Entity:FireBullets( bullet )
											return
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end

function ENT:Think()
	self.Entity:NextThink(CurTime() + 2)
	if self.core then
		if  (self.core:IsValid()) then	
			self:GetEnemyandFire()
		else
			self.core = nil
		end
	else
		self.core = getshipcore(self.Entity)
		if not self.core then
			self.Entity:NextThink( CurTime() + 5 )
			return true
		end
	end
	return true
end


function ENT:OnTakeDamage(info)
	Shieldhit(info,self.Entity)
end
